package com.example.demo.controller;

import com.example.demo.common.AppResult;
import com.example.demo.exception.ApplicationException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//返回JSON格式的Controller
@RestController()
//指定测试根路径映射地址前缀
@RequestMapping("/test")
//API 描述信息
@Api(tags="TestController 测试接口")

public class TestController {

    //API方法描述
    @ApiOperation(value="测试接口1，打印hello")
    //地址映射
    @GetMapping("/hello")
    // 具体⽅法实现
    public String hello() {
        return "Hello, Spring Boot...";
    }
    // 注意：@ApiParam ⽅法参数的注解，⽤来描述参数
    @ApiOperation(value = "测试接⼝3，返回 你好+传⼊参数")
    @GetMapping("/helloByName")
    public AppResult<String> helloByName(@ApiParam(value = "名字", required =
            true) String name) {
        return AppResult.success("你好：" + name);
    }

    @GetMapping("/exception")
    public AppResult testException()  throws Exception{
        throw new Exception("这是一个Exception");
    }

    @GetMapping("/appException")
    public AppResult testApplicationException(){
        throw new ApplicationException("这是一个Application");
    }
}
